//
//  MySqlite.m
//  FMdb
//
//  Created by 沈成才 on 15-4-27.
//  Copyright (c) 2015年 孙俊夫. All rights reserved.
//

#import "MySqlite.h"
#import "Student.h"
#import "Teacher.h"
@implementation MySqlite
// 打开数据库

+(FMDatabase *)openMySqlite:(NSString *)name
{
    
    //paths: ios下Document路径，Document为ios中可读写的文件夹
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask
                                                        , YES) lastObject];
    // 拼接文件夹路径
    NSString *dbPath = [path stringByAppendingPathComponent:name];
    NSLog(@"路径========%@",dbPath);
    // 创建数据库实例
    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    if ([db open]) {
        NSLog(@"打开数据库成功！");
        return db;
        
    }else
    {
        NSLog(@"打开数据库失败！");
        return nil;
    }
    
    
}
// 创建表
+(BOOL)createTableWith:(NSString *)sql
{
    FMDatabase *db = [MySqlite openMySqlite:@"student.db"];
    BOOL result = [db executeUpdate:sql];
    [db close];
    if (result) {
        NSLog(@"创建表成功");
        return result;
            }else{
        
        NSLog(@"创建表失败");
        return result;
    }
    
    
}

+(BOOL)createTableTeacher:(NSString *)sql
{
    
    FMDatabase *db = [MySqlite openMySqlite:@"teacher.db"];
    BOOL result = [db executeUpdate:sql];
    [db close];
    if (result) {
        NSLog(@"创建表成功");
        return result;
    }else{
        
        NSLog(@"创建表失败");
        return result;
    }

    
}

//插入学生数据
+ (BOOL)insertStuWithSql:(NSString *)sql
{
    FMDatabase *db = [MySqlite openMySqlite:@"student.db"];
    BOOL result = [db executeUpdate:sql];
    [db close];
    if (result) {
        NSLog(@"插入数据成功");
        return result;
    }else
    {
        NSLog(@"插入数据失败");
        return result;
    }
    
}

+ (BOOL)insertTeaWithSql:(NSString *)sql
{
    FMDatabase *db = [MySqlite openMySqlite:@"teacher.db"];
    BOOL result = [db executeUpdate:sql];
    [db close];
    if (result) {
        NSLog(@"插入数据成功");
        return result;
    }else
    {
        NSLog(@"插入数据失败");
        return result;
    }
    
}



//查询
+(NSMutableArray *)selectStuWitSql:(NSString *)sql
{
    FMDatabase *db = [MySqlite openMySqlite:@"student.db"];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    FMResultSet *result = [db executeQuery:sql];
    while (result.next) {
        Student *stu = [[Student alloc] init];
        stu.name = [result stringForColumn:@"name"];
        stu.age = [result intForColumn:@"age"];
        [arr addObject:stu];
        
        
        
        
    }
    
    return arr;
}

+(NSMutableArray *)selectTeaWitSql:(NSString *)sql
{
    FMDatabase *db = [MySqlite openMySqlite:@"teacher.db"];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    FMResultSet *result = [db executeQuery:sql];
    while (result.next) {
        Teacher *tea = [[Teacher alloc] init];
        tea.name = [result stringForColumn:@"name"];
        tea.hobby = [result stringForColumn:@"hobby"];
        [arr addObject:tea];
        
        
        
        
    }
    
    return arr;
}
// 删除老师表

+(BOOL)deleteTableTea:(NSString *)sql
{
    FMDatabase *db = [MySqlite openMySqlite:@"teacher.db"];
    BOOL result = [db executeUpdate:sql];
    [db close];
    if (result ) {
        NSLog(@"删除表成功");
        return result;
    }else
    {
        NSLog(@"删除表失败");
        return  result;
    }
    
    
}



@end
